package com.example.zhinongbao.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.zhinongbao.Fragment.NewsFragment;
import com.example.zhinongbao.R;
import com.example.zhinongbao.adapter.ViewPagerAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;

public class NewsActivity extends AppCompatActivity {
    private ViewPager2 viewPager;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);

        // 设置 Toolbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("新闻资讯");

        // 初始化 Tab 栏
        viewPager = findViewById(R.id.viewPager);
        tabLayout = findViewById(R.id.tabLayout);

        setupViewPager();

        // 关联 TabLayout 和 ViewPager2
        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            switch (position) {
                case 0: tab.setText("推荐"); break;
                case 1: tab.setText("热点"); break;
                case 2: tab.setText("农业"); break;
                case 3: tab.setText("行情"); break;
                case 4: tab.setText("创业项目"); break;
            }
        }).attach();
    }

    private void setupViewPager() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new NewsFragment("推荐"));
        fragments.add(new NewsFragment("热点"));
        fragments.add(new NewsFragment("农业"));
        fragments.add(new NewsFragment("行情"));
        fragments.add(new NewsFragment("创业项目"));

        ViewPagerAdapter adapter = new ViewPagerAdapter(this, fragments);
        viewPager.setAdapter(adapter);
    }
}